package com.jingogo.controller;

import com.jingogo.common.Result;
import com.jingogo.service.RoleService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotNull;

@Slf4j
@RestController
@RequestMapping("/role")
@Tag(name = "角色权限管理", description = "后台用户与角色管理接口")
public class RoleController {
    @Autowired
    private RoleService roleService;

    @PostMapping("/addUser")
    @Operation(summary = "后台添加用户", description = "后台添加新用户")
    @PreAuthorize("hasRole('ADMIN')")
    public Result<String> addUser(@RequestParam @NotNull String username, @RequestParam @NotNull String password, @RequestParam @NotNull String role) {
        roleService.addUser(username, password, role);
        return Result.success("用户添加成功");
    }

    @PostMapping("/assignRole")
    @Operation(summary = "分配角色", description = "为用户分配角色")
    @PreAuthorize("hasRole('ADMIN')")
    public Result<String> assignRole(@RequestParam @NotNull Long userId, @RequestParam @NotNull String role) {
        roleService.assignRole(userId, role);
        return Result.success("角色分配成功");
    }

    @GetMapping("/list")
    @Operation(summary = "角色列表", description = "获取所有角色")
    @PreAuthorize("hasRole('ADMIN')")
    public Result<?> listRoles() {
        return Result.success(roleService.listRoles());
    }
} 